package com.sig;

import java.util.ArrayList;
import java.util.List;

import com.sig.core.*;
import com.sig.opengl.*;
import com.sig.slider.Slider;
import com.sig.tabs.*;

import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.GravityCompat;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;;

public class MainActivity extends FragmentActivity{

	PagesAdapter _pagesAdapter;
	ViewPager _pager;

	List<com.sig.core.Tab> _pages = new ArrayList<com.sig.core.Tab>();

	private DrawerLayout _page;

	private View slider_layout;
	private ActionBarDrawerToggle slider;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		creerTab();
		instancier_slider();
	}

	private void creerTab(){

		_pages.add(new TabHome());
		_pages.add(new Tab1());
		_pages.add(new OpengGLFragment());
		_pages.add(new OpengGLFragment());


		_pagesAdapter = new PagesAdapter(getSupportFragmentManager(),_pages);
		_pager = (ViewPager) findViewById(R.id.pager);

		_pager.setAdapter(_pagesAdapter);
	}


	public void instancier_slider(){
		//active le bouton home en haut a gauche
		getActionBar().setHomeButtonEnabled(true);
		//active le bouton retour
		getActionBar().setDisplayHomeAsUpEnabled(true);

		_page = (DrawerLayout) findViewById(R.id.layout_global);
		slider_layout = (ViewGroup) findViewById(R.id.slider);

		_page.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);

		slider = new Slider(this,_page);
		_page.setDrawerListener(slider);
	}

	public boolean onOptionsItemSelected(MenuItem item) {
		switch(item.getItemId()) {
		case android.R.id.home :
			if (_page.isDrawerOpen(slider_layout)) {
				_page.closeDrawer(slider_layout);
			} else {
				_page.openDrawer(slider_layout);
			}
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	// Met a jour le fragment central
	public void selectItem(String item) {
		/*
		SherlockFragment new_center_fragment = new CenterFragment();
		Bundle args = new Bundle();
		args.putString(CenterFragment.ARGUMENT, item);
		new_center_fragment.setArguments(args);

		FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
		ft.replace(R.id.content_frame, new_center_fragment);
		ft.commit();
		 */
		_page.closeDrawer(slider_layout);
	}
	
	
	protected void onPostCreate(Bundle savedInstanceState) {
		super.onPostCreate(savedInstanceState);
		slider.syncState();
	}

	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
		slider.onConfigurationChanged(newConfig);
	}
	
}
